﻿using System.Globalization;
using System.Text;
using Sanelib.Common.SystemSettings;

namespace Sanelib.CodeMaker.Generators
{
    public class AddProcessorGenerator
    {
        public static void Generate(DatabaseTableSchema tbSchema)
        {
            var sb = new StringBuilder();

            DefaultHeader(tbSchema, sb);
            CreateProcessorBody(tbSchema, sb);
            DefaultFooter(sb);

            System.IO.File.WriteAllText(Globals.CreateOrGetCustomPath("..\\Core\\Processors\\Add") + tbSchema.TableName + "Processor_Generated.cs", sb.ToString());
        }

        private static void DefaultHeader(DatabaseTableSchema tbSchema, StringBuilder sb)
        {
            var textInfo = new CultureInfo("en-US", false).TextInfo;

            sb.AppendLine("using System;");
            sb.AppendLine("using Common.Commands;");
            sb.AppendLine("using Common.Commands."+tbSchema.TableName+"Commands;");
            sb.AppendLine("using Core.Domain.Model;");
            sb.AppendLine("using Sanelib.Common.Helpers;");
            sb.AppendLine("using Sanelib.DataOperation;");
            sb.AppendLine("using Sanelib.Dto;");
            sb.AppendLine("");
            sb.AppendLine("namespace Core.Processors." + tbSchema.TableName + "Processors");
            sb.AppendLine("{");

            sb.AppendLine("\tpublic class Add" + textInfo.ToTitleCase(tbSchema.TableName) + "Processor : ICommandProcessor<Add" + textInfo.ToTitleCase(tbSchema.TableName) + ">");
            sb.AppendLine("\t{");
            sb.AppendLine("\t\tprivate readonly IRepository<" + textInfo.ToTitleCase(tbSchema.TableName) + "> _" + textInfo.ToLower(tbSchema.TableName) + "Repository;");

            sb.AppendLine("");
            sb.AppendLine("\t\tpublic Add" + textInfo.ToTitleCase(tbSchema.TableName) + "Processor(IRepository<" + textInfo.ToTitleCase(tbSchema.TableName) + "> " + textInfo.ToLower(tbSchema.TableName) + "Repository)");
            sb.AppendLine("\t\t{");
            sb.AppendLine("\t\t\t_" + textInfo.ToLower(tbSchema.TableName) + "Repository = " + textInfo.ToLower(tbSchema.TableName) + "Repository;");
            sb.AppendLine("\t\t}");

            sb.AppendLine("");
            sb.AppendLine("\t\tpublic dynamic Process(Add" + textInfo.ToTitleCase(tbSchema.TableName) + " command)");
            sb.AppendLine(" \t\t{");
            sb.AppendLine("\t\t\tvar response = new WebApiResultResponse();");
        }

        private static void CreateProcessorBody(DatabaseTableSchema tbSchema, StringBuilder sb)
        {
            var textInfo = new CultureInfo("en-US", false).TextInfo;

            sb.AppendLine("");
            sb.AppendLine("\t\t\tvar " + textInfo.ToLower(tbSchema.TableName) + " = new " + textInfo.ToTitleCase(tbSchema.TableName));
            sb.AppendLine("\t\t\t{ ");
            sb.AppendLine("\t\t\t\tId = command.Id,");
            foreach (var item in tbSchema.Columns)
            {
                sb.AppendLine("\t\t\t\t" + item.PropertyName + " = command." + item.PropertyName + ",");
            }

            sb.AppendLine("\t\t\t}; ");
            sb.AppendLine("\t\t\t_" + textInfo.ToLower(tbSchema.TableName) + "Repository.Add(" + textInfo.ToLower(tbSchema.TableName) + ");");

            sb.AppendLine("");
            sb.AppendLine("\t\t\tresponse.IsSuccess = true;");
            sb.AppendLine("");
            sb.AppendLine("\t\t\treturn response;");
        }

        public static void DefaultFooter(StringBuilder sb)
        {
            sb.AppendLine("\t\t}");
            sb.AppendLine("\t}");
            sb.AppendLine("}");
        }
    }
}
